package endereco;

import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

import persistencia.RNException;

@ManagedBean(name = "enderecoBean")
@RequestScoped
public class EnderecoBean {
	
	private Endereco endereco = new Endereco();
	@SuppressWarnings("unused")
	private List<Endereco> lista; 

	public Endereco getEndereco() {
		return endereco;
	}

	public void setEndereco(Endereco endereco) {
		this.endereco = endereco;
	}
	
	public String salvar() {
		try {
			EnderecoRN enderecoRN = new EnderecoRN();
			enderecoRN.salvar(endereco);
			return "clienteSalvo";
		} catch (RNException e) {
			e.printStackTrace();

		}
		return "falha";
	}
	
	public List<Endereco> getLista() {

		EnderecoRN enderecoRN = new EnderecoRN();
		try {
			return enderecoRN.listar();

		} catch (RNException e) {
			e.printStackTrace();
			return null;
		}

	}
	
	@SuppressWarnings("unused")
	public void buscaPorCep(){
		FacesContext contexto = FacesContext.getCurrentInstance();
		String cep = this.endereco.getCep();
		System.out.println(cep);
		EnderecoRN enderecoRN = new EnderecoRN();
		this.endereco = enderecoRN.buscaPorCep(cep);
		
		System.out.println(endereco.getEndereco());
		
		
	}

}
